﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.ViewModels;

namespace kzjd.rycp.WebApp.App_Start
{
    /// <summary>
    /// 跳过授权检查
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public sealed class EvalAuthorizeFilterAttribute : ActionFilterAttribute
    {
        private bool IsJsonResult = false;

        public EvalAuthorizeFilterAttribute()
        {
            this.IsJsonResult = false;
        }

        public EvalAuthorizeFilterAttribute(bool jsonResult)
        {
            this.IsJsonResult = jsonResult;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (EvalMemberSession.Member == null)
            {
                var backurl = "";
                if (filterContext.HttpContext != null
                    && filterContext.HttpContext.Request != null)
                {
                    backurl = filterContext.HttpContext.Request.RawUrl;
                }

                if (this.IsJsonResult)
                {
                    var result = new JsonResult();
                    result.Data = new AjaxResult(AjaxResult.RESULT_SESSION_TIMEOUT, "登录过期");
                    filterContext.Result = result;
                }
                else
                {
                    filterContext.Result = new RedirectResult("~/Eval/Login?backurl=" + System.Web.HttpUtility.UrlEncode(backurl));
                }
            }
        }
    }
}